package com.rency.mtds.manager.helper;

import com.google.common.collect.Lists;
import com.rency.mtds.manager.dal.entity.DataSourcePO;
import com.rency.mtds.manager.spi.entity.DataSourceBO;
import com.rency.mtds.manager.spi.enums.DbType;
import com.rency.mtds.manager.spi.enums.MasterSlaveType;
import org.apache.commons.collections.CollectionUtils;
import org.rency.halo.framework.lang.enums.YesNo;

import java.util.List;
import java.util.Objects;

/**
 * @author: caiyu.ren
 * Created on 2019/10/11 9:21
 * @version: V1.0.0
 * @since JDK 11
 */
public class ConvertHelper {

    public static DataSourcePO convertDataSourceBO(DataSourceBO info){
        if(Objects.isNull(info)){
            return null;
        }
        DataSourcePO entity = new DataSourcePO();
        entity.setAppId(info.getAppId());
        entity.setAppName(info.getAppName());
        entity.setDbType(Objects.isNull(info.getDbType()) ? null : info.getDbType().getCode());
        entity.setDsConnectProperties(info.getDsConnectProperties());
        entity.setDsDriver(info.getDsDriver());
        entity.setDsPasswd(info.getDsPasswd());
        entity.setDsProperties(info.getDsProperties());
        entity.setDsType(info.getDsType());
        entity.setDsUrl(info.getDsUrl());
        entity.setDsUser(info.getDsUser());
        entity.setEnable(Objects.isNull(info.getEnable()) ? null : info.getEnable().getCode());
        entity.setGmtCreate(info.getGmtCreate());
        entity.setGmtModified(info.getGmtModified());
        entity.setId(info.getId());
        entity.setMsKind(Objects.isNull(info.getMsType()) ? null : info.getMsType().getCode());
        return entity;
    }

    public static List<DataSourceBO> convertDataSourcePO(List<DataSourcePO> entities){
        if(CollectionUtils.isEmpty(entities)){
            return Lists.newArrayList();
        }
        List<DataSourceBO> list = Lists.newArrayListWithCapacity(entities.size());
        entities.forEach( entity -> list.add(ConvertHelper.convertDataSourcePO(entity)));
        return list;
    }

    public static DataSourceBO convertDataSourcePO(DataSourcePO entity){
        if(Objects.isNull(entity)){
            return null;
        }
        DataSourceBO info = new DataSourceBO();
        info.setAppId(entity.getAppId());
        info.setAppName(entity.getAppName());
        info.setDbType(DbType.get(entity.getDbType()));
        info.setDsConnectProperties(entity.getDsConnectProperties());
        info.setDsDriver(entity.getDsDriver());
        info.setDsPasswd(entity.getDsPasswd());
        info.setDsProperties(entity.getDsProperties());
        info.setDsType(entity.getDsType());
        info.setDsUrl(entity.getDsUrl());
        info.setDsUser(entity.getDsUser());
        info.setEnable(YesNo.get(entity.getEnable()));
        info.setGmtCreate(entity.getGmtCreate());
        info.setGmtModified(entity.getGmtModified());
        info.setId(entity.getId());
        info.setMsType(MasterSlaveType.get(entity.getMsKind()));
        return info;
    }
}
